package com.etrans.web.gps.business.command.service;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etrans.egov.base.intf.TCmdSendResp;
import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.EgovUtil;

/**
 * 定位服务-业务层
 *
 * @author Jason.S
 */
@Service
public class CommandService extends BaseService {
    private final static Logger logger = LoggerFactory.getLogger("MonitorService");

    @Autowired
    private EgovUtil egovUtil;
    @Autowired
    private Config config;

    /**
     * 发送指令
     *
     * @param params
     * @return
     * @throws NumberFormatException
     * @throws Exception
     */
    public Map<String, Object> sendCommand(Map<String, Object> params) throws NumberFormatException, Exception {
//        UUID uuid = UUID.randomUUID();
//        long commandTime = System.currentTimeMillis();
//        TCmdSendResp result = egovUtil.sendCMD(Integer.valueOf(params.get("userId").toString()), uuid, commandTime,
//                Integer.valueOf(params.get("vehicleId").toString()), Integer.valueOf(params.get("terminalId").toString()),
//                Integer.valueOf(params.get("terminalType").toString()), params.get("commno").toString(),
//                Integer.valueOf(params.get("commandId").toString()), params.get("cmdArgs").toString());
//        Map<String, Object> resultMap = new HashMap<>();
//        if (result != null) {
//            resultMap.put("userId", result.getUserID());
//            resultMap.put("uuid", result.getReqUUID());
//            resultMap.put("respTime", result.getRespTime());
//            resultMap.put("flag", result.getRespType().getFlag());
//            resultMap.put("resultName", result.getRespType().getName());
//            resultMap.put("cmdPkgSeq", result.getCmdPkgSeq());
//            resultMap.put("commandId", params.get("commandId").toString());
//            resultMap.put("vehicleId", params.get("vehicleId").toString());
//            resultMap.put("commandTime", commandTime);
//            resultMap.put("result", 0);
//        } else {
//            resultMap.put("result", 1);
//        }
//        return resultMap;
    	return null;
    }

    public TCmdSendResp sendCommandGetTCmdSendResp(Map<String, String> params) throws NumberFormatException, Exception {
//        long commandTime = System.currentTimeMillis();
//        String _req_uuid = params.get("_req_uuid");
//        long _req_time = Long.valueOf(params.get("_req_time"));
//        int _user_id = Integer.parseInt(params.get("_user_id"));
//        int _vehicle_id = Integer.parseInt(params.get("_vehicle_id"));
//        String _plate_num = params.get("_plate_num");
//        int _plate_color = Integer.parseInt(params.get("_plate_color"));
//        int _cmd_id = Integer.parseInt(params.get("_cmd_id"));
//        String _cmd_args = params.get("_cmd_args");
//        
//        UReqObj reqObj = new UReqObj(_req_uuid, _req_time, _user_id, _vehicle_id, _plate_num, _plate_color, _cmd_id, _cmd_args);
//        TCmdSendResp result = egovUtil.sendCMD(reqObj);
//        return result;
    	return null;
    }

    /**
     * 指令回复
     *
     * @param params
     * @return
     * @throws Exception
     */
    public Map<String, Object> getCommandReply(Map<String, Object> params) throws Exception {
//        TCmdReply result = null;
//        result = egovUtil.getReply(Integer.valueOf(params.get("terminaltype").toString()),
//                params.get("commno").toString(), Integer.valueOf(params.get("commandId").toString()),
//                Integer.valueOf(params.get("cmdPkgSeq").toString()), 30);
        Map<String, Object> resultMap = new HashMap<>();
//        if (result != null) {
////			System.out.println(params.get("commandId").toString());
//            if ("34817".equals(params.get("commandId").toString())) {  // 指令回复
//                TIntfObjImage image = egovUtil.getImage(params.get("commno").toString(), Integer.valueOf(params.get("terminalId").toString()), Long.valueOf(params.get("commandTime").toString()), 300);
//                if (image != null) {
//                    resultMap.put("imageUrl", config.imagePath + image.getImageUrl());
//                    System.out.println("-----IMAGE------" + image.getImageUrl());
//                }
//            }
//            resultMap.put("replyCode", params.get("commandId").toString());
//            resultMap.put("commandId", params.get("commandId").toString());
//            resultMap.put("result", result.isCmdOK());
//            resultMap.put("replyBody", result.getReplyBody());
//            resultMap.put("commno", params.get("commno").toString());
//            resultMap.put("uuid", params.get("uuid").toString());
//        } else {
//            resultMap.put("result", false);
//        }
        return resultMap;
    }
    /**
     * 运维版指令回复
     *
     * @param params
     * @return
     * @throws Exception
     */

//    public Map<String,Object> getCommandReplyOps(Map<String, String> params) throws Exception {
//        logger.info("指令回复请求参数 {}", JSON.toJSONString(params));
//        TCmdReply replay =  egovUtil.getReply(Integer.valueOf(params.get("terminaltype")),
//                params.get("commno"),
//                Integer.valueOf(params.get("commandId")),
//                Integer.valueOf(params.get("cmdPkgSeq")),
//                Integer.parseInt(params.get("_time_out_seconds")));
//        Map<String,Object> resultMap = new HashMap<>();
//        if(replay != null){
//            if("34817".equals(params.get("commandId").toString())){  // 指令回复
//                TIntfObjImage image =  egovUtil.getImage(params.get("commno").toString(),Integer.valueOf(params.get("terminalId").toString()),Long.valueOf(params.get("commandTime").toString()),20);
//                if(image != null){
//                    resultMap.put("imageUrl",config.imagePath+image.getImageUrl());
//                    logger.info("{},{},{}",params.get("commno"),params.get("commandId"),image.getImageUrl());
//                }else{
//                    logger.warn("{},{} image is null",params.get("commno"),params.get("commandId"));
//                }
//            }
//            resultMap.put("replyCode",params.get("commandId").toString());
//            resultMap.put("commandId", params.get("commandId").toString());
//            resultMap.put("result",replay.isCmdOK());
//            resultMap.put("resp_time",replay.getCaUpTime());//终端应答时间
//            resultMap.put("respBody", replay.getReplyBody());
//            resultMap.put("respMsg", replay.getReplyMsg());
//            resultMap.put("commno",params.get("commno").toString());
//            resultMap.put("caUpData",replay.getCaUpData());
//        }else{
//            resultMap.put("result",false);//没有渠道回复信息，前段继续轮训
//        }
//        return resultMap;
//    }

    /**
     * 处理报警
     *
     * @throws Exception
     */
    public void clearAlarm(String[] commnos) throws Exception {
//        egovUtil.clearAlarm(commnos);
    }

    /**
     * 处理报警
     *
     * @throws Exception
     */
    public void clearAlarm(String commno) throws Exception {
        String[] commnos = new String[]{commno};
        this.clearAlarm(commnos);
    }

    /**
     * 查看是否存在报警
     *
     * @throws Exception
     */
//    public List<TIntfObjAlarm> getAlarm(String[] commnos) throws Exception {
//        return egovUtil.getAlarm(commnos);
//    }

    /**
     * 获取指令下发状态
     */
//    public TCmdStepInfo getCmdStepInfo(UUID _req_uuid, int _timeout_secs) {
//        return this.egovUtil.getCmdStepInfo(_req_uuid, _timeout_secs);
//    }

    /**
     * 查看是否存在报警
     *
     * @throws Exception
     */
//    public List<TIntfObjAlarm> getAlarm(String commno) throws Exception {
//        String[] commnos = new String[]{commno};
//        return getAlarm(commnos);
//    }

    /**
     * 查看报警数据
     */
//    public List<TIntfObjAlarmT> getAlarmT(Set<Integer> _alarmkindids, String... _commnos) {
//        return this.egovUtil.getAlarmT(_alarmkindids, _commnos);
//    }
}
